package engine.display.newAttack
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import newx.com.ResManager;
	import newx.utils.BitmapAPI;
	
	public class ShowAttackDamage extends Sprite
	{
		public static const HP 					: String = "hp";
		public static const MP 					: String = "mp";
		public static const BUFFERDAMAGE 			: String = "bufferdamage";
		
		//暴击伤害
		public static const ERUPT_DEDUCT_HP		: String = "erupt_deduct_hp";
		public static const DEDUCT_HP				: String = "deduct_hp";
		public static const DEDUCT_MP				: String = "deduct_mp";
		public static const DEDUCT_BUFFERDAMAGE	: String = "deduct_bufferdamage";
		
		public static const ADD_HP 				: String = "add_hp";
		public static const ADD_MP 				: String = "add_mp";
		public static const ADD_BUFFERDAMAGE 		: String = "add_bufferdamage";
		
		public static const STR					: String = "str";
		public static const BUFFER				: String = "buffer";
			
		private var _bitMap	:Bitmap		=	new Bitmap();
		
		private var _isUse		:Boolean	=	true;
		
		private var _pos		:Point		=	new Point();
		
		private var _width		:int;
		
		//hp/内力/跳字
		private var _effect	:String;
		
		private var _timeline	:TimelineMax;
		
		private static var attackDamages	:Array	=	[];
		
		private static var _numObj		:Object	=	{};
		
		public function ShowAttackDamage()
		{	
			addChild(_bitMap);
			mouseChildren = mouseEnabled = false;
		}
		
		private static function getFreeDamageEffect():ShowAttackDamage
		{
			var showDamage : ShowAttackDamage;
			for each( var effect : ShowAttackDamage in attackDamages )
			{
				if(!effect._isUse)
				{
					effect._isUse	=	true;
					showDamage =  effect;
					break;
				}
			}
			
			if( null == showDamage )
			{
				showDamage = new ShowAttackDamage();
				attackDamages.push( showDamage);
			}
			
			showDamage.clear();
			
			return showDamage;
		}
		
		/**
		 * @param type		hp/内力/跳字  1,2,3
		 * @param result	1,普通攻击;2,暴击;3,招架4,格挡;5,闪避,6加血;7回气;8降气; -->）
		 * @param data		值
		 * 
		 */
		public static function ShowDamageEffect( effect : String, result : int, data : int, p : Point  ):void
		{
			if( 0 == data )return;
			
			var tDamge : ShowAttackDamage =	getFreeDamageEffect();
			
			if( HP == effect )
			{
				if( data > 0 )
				{//扣血
					tDamge._effect = DEDUCT_HP;
					if( 2 == result )
					{
						tDamge.showNum( 13, result, data );
						tDamge._effect = ERUPT_DEDUCT_HP;
					}else
					{
						tDamge.showNum( 14, result, data );
					}
				}else
				{
					tDamge.showNum( 15, result, data );
					tDamge._effect = ADD_HP;
				}
			}else if( MP == effect )
			{
				if( data > 0 )
				{//扣气势
					tDamge.showNum( 17, result, data );
					tDamge._effect = DEDUCT_MP;
				}else
				{
					tDamge.showNum( 16, result, data );
					tDamge._effect = ADD_MP;
				}
			}else
			{
//				tDamge.showNum( 1, result, data );
				if( data > 0 )
				{//扣buff血或者蓝
					tDamge.showNum( 14, result, data );
					tDamge._effect = DEDUCT_HP;
				}else
				{
					tDamge.showNum( 15, result, data );
					tDamge._effect = ADD_HP;
				}
			}
			show( tDamge, p );
		}
		
		public static function ShowTextEffect( result : int, p : Point ):void
		{
			var tDamge : ShowAttackDamage =	getFreeDamageEffect();
			
			tDamge.showText( result );
			
			tDamge._effect = STR;
			
			show( tDamge, p );
		}
		
		public static function ShowBuffEffect( type : int, result : int, p : Point ):void
		{
			var tDamge : ShowAttackDamage =	getFreeDamageEffect();
			
			tDamge.showBuff( type );
			
			if( !tDamge._isUse )return;
			
			tDamge._effect = BUFFER;
			
			show( tDamge, p );
		}
		
		private static function show( tDamge : ShowAttackDamage, p : Point ):void
		{
			tDamge.y	=	p.y;
			tDamge.x 	= 	p.x - tDamge._width * .5;
			
			AttackLogic.getInstance().attackScene.damageSpace.addChild(tDamge);
			
			tDamge.playerEffect();
		}
		
		private function showNum( type : int, result : int, data : int ):void
		{
			var dataArr	:Array	=	String( Math.abs( data ) ).split("");
			var leng	:int	=	dataArr.length;
			
			var sign : String = "";
			switch( type )
			{//跳血的加减号
				case 13:
					sign = "暴击";
					break;
				case 14:
					sign = "减血";
					break;
				case 15:
					sign = "加血";
					break;
				case 16:
					sign = "加气";
					break;
				case 17:
					sign = "减气";
					break;
			}
			
			//符号
			var signBmd:BitmapData = getRes( sign );
			
			//第一个数字
			var bmd		:BitmapData = getRes( type + dataArr[ 0 ] );
			
			if( type == 13 )
			{
				_bitMap.bitmapData = new BitmapData( (bmd.width + 10 ) * leng + signBmd.width, bmd.height + 20, true, 0 );

			}else
			{
				_bitMap.bitmapData = new BitmapData( (bmd.width + 2 ) * leng + 40 + signBmd.width, bmd.height + 20, true, 0 );
			}
			
			_pos.x	= 0;
			_pos.y	= ( bmd.height - signBmd.height ) * .5;
			_bitMap.bitmapData.copyPixels( signBmd, _bitMap.bitmapData.rect, _pos );
			
			_pos.x	=	signBmd.width + 2;
			_pos.y	=	0;
			_bitMap.bitmapData.copyPixels( bmd, _bitMap.bitmapData.rect, _pos );
			
			for( var i : int = 1; i < leng; i++ )
			{
				_pos.x += bmd.width + i + 1;
				bmd = getRes( type + dataArr[i] );
				
				_bitMap.bitmapData.copyPixels( bmd, bmd.rect, _pos );
			}
			
			_width = _bitMap.width;
		}
		
		private function showBuff( result : int ):void
		{//1防御上升2格挡上升3招架上升4防御下降5普工上升6普工下降7绝技上升8绝技下降9昏眩10回气11降气12回血
			var key : String = "";
			switch( result )
			{
				case 1 :
					//防御上升
					key = "46";
					break;
				case 2 :  
					//格挡上升
					key = "45";
					break;
				case 3 :  
					//招架上升4虚招
					key = "58";
					break;
				case 4 :  
					//防御下降
					key = "44";
					break;
				case 5 :  
					//普工上升
					key = "55";
					break;
				case 6 :  
					//普工下降
					key = "60";
					break;
				case 7 :  
					//绝技上升
					key = "56";
					break;
				case 8 :  
					//绝技下降
					key = "59";
					break;
				case 9 :  
					//昏眩
//					key = "48";
					break;
				case 10 :  
					//回气
					key = "54";
					break;
				case 11 :  
					//降气
					key = "53";
					break;
				case 12 : 
					//回血
					break;
				case 13 : 
					//攻击下降
					key = "48";
					break;
				case 14 : 
					//暴击上升
					key = "43";
					break;
				case 15 : 
					//闪避上升
					key = "57";
					break;
				case 16:
					//属性提升
					key = "61";
			}
			
			if( "" != key )
			{
				_pos.x = 0;
				_bitMap.bitmapData = new BitmapData( 170, 70, true, 0 );
				var bmd		:BitmapData = getRes( key );
				
				_bitMap.bitmapData.copyPixels( bmd, bmd.rect, _pos );
				_width = _bitMap.width;
			}else
			{
				onDisappear();
			}
		}
		
		private function showText( result : int ):void
		{//1,普通攻击;2,暴击;3,招架4,格挡;5,闪避; -->）
			var key : String;
			if( 2 == result )
			{//2暴击
				key = "40";
			}else if( 5 == result )
			{//5闪避
				key = "41";
			}else if( 3 == result )
			{//3,招架
				key = "52";
			}else if( 4 == result )
			{//4,格挡
				key = "42";
			}
			
			var bmd		:BitmapData = getRes( key );
			_bitMap.bitmapData = new BitmapData( bmd.width, bmd.height, true, 0 );
			_bitMap.bitmapData.copyPixels( bmd, _bitMap.bitmapData.rect, new Point( 0, 0 ) );
			
			_width = _bitMap.width;
		}
		
		public function clear():void
		{
			if(_bitMap.bitmapData != null)
			{
				_bitMap.bitmapData.dispose();
			}
		}
		
		/**
		 *	获得数字 
		 * @param key
		 * @return 
		 * 
		 */
		private function getRes(key : String):BitmapData
		{
			if( null != _numObj[ key ] )
			{
				return _numObj[ key ]
			}
			
			var pic : BitmapData = BitmapAPI.getBitmapData( ResManager.newResClass( "fig" + key ) );
			_numObj[ key ] = pic;
			
			return pic;
		}
		
		private function playerEffect():void
		{
			alpha	=	0;
			scaleX	=	1;//ease:Circ.easeOut
			scaleY	=	1;
			
			_timeline		=	new TimelineMax();
			switch( _effect )
			{
				case ERUPT_DEDUCT_HP:
				{//暴击扣血
					scaleX	=	0;
					scaleY	=	0;
					_timeline.append(new TweenLite(this, .2, { y:y-120, x:x-_width * .5, scaleX:2, scaleY:2, alpha:1}));
					_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, x:x+_width*.06, scaleX:.7, scaleY:.7, onComplete:onDisappear}));
					break;
				}
				case DEDUCT_HP:
				{//扣血
					_timeline.append(new TweenLite(this, .5, { y:y-120, alpha:1}));
					_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, onComplete:onDisappear}));
					break;
				}
				case DEDUCT_MP:
				{//扣蓝
					_timeline.append(new TweenLite(this, .4, { delay:.2, y:y-120, alpha:1}));
					_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, onComplete:onDisappear}));
					break;
				}
				case DEDUCT_BUFFERDAMAGE:
				{//扣buff血
					_timeline.append(new TweenLite(this, .3, { delay:.2, y:y-120, alpha:1}));
					_timeline.append(new TweenLite(this, .2, { delay:.2, alpha:0, onComplete:onDisappear}));
					break;
				}
				case ADD_HP:
				{//加血
					_timeline.append(new TweenLite(this, .5, { y:y-120, alpha:1}));
					_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, onComplete:onDisappear}));
					break;
				}
				case ADD_MP:
				{//加蓝
					_timeline.append(new TweenLite(this, .4, { delay:.2, y:y-120, alpha:1}));
					_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, onComplete:onDisappear}));
					break;
				}
				case ADD_BUFFERDAMAGE:
				{//加buff血
					_timeline.append(new TweenLite(this, .3, { delay:.2, y:y-120, alpha:1}));
					_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, onComplete:onDisappear}));
					break;
				}
				case BUFFER:
				{//跳buff
					scaleX	=	0;
					scaleY	=	0;
					_timeline.append(new TweenLite(this, .1, { delay:.2, y:y-200, scaleX:1, scaleY:1, alpha:1}));
					_timeline.append(new TweenLite(this, .4, { delay:.3, alpha:0, x:x+_width*.15, scaleX:.7, scaleY:.7, onComplete:onDisappear}));
					break;
				}
				case STR:
				{//跳防御等字
					scaleX	=	0;
					scaleY	=	0;
					_timeline.append(new TweenLite(this, .7, { delay:.1, y:y-120, scaleX:1, scaleY:1, alpha:1}));
					_timeline.append(new TweenLite(this, .1, { delay:.2, alpha:0, x:x+_width*.15, scaleX:.7, scaleY:.7, onComplete:onDisappear}));
					break;
				}
			}
			_timeline.play();
		}
		private function onDisappear():void
		{
			if(_timeline != null)
			{
				_timeline.clear();
				_timeline.kill();
			}
			
			if(this.parent!=null)
			{
				parent.removeChild(this);
			}
			
			_isUse	=	false;
		}
	}
}